package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.MLApplication;
import com.pocketsweet.MLContext;
import com.pocketsweet.R;
import com.pocketsweet.model.MLRecommendUser;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Profile;
import com.pocketsweet.ui.TempProfile;
import com.pocketsweet.ui.uilib.NumberToChinese;

/**
 * 
 * @author Kam
 * 
 */
public class TopicListAdapter extends BaseListAdapter<MLRecommendUser> {

	public TopicListAdapter(Context ctx) {
		super(ctx);
	}

	public TopicListAdapter(Context ctx, List<MLRecommendUser> datas) {
		super(ctx, datas);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_recommend_topic,
					parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	private class ViewHolder implements View.OnClickListener {
		ImageView imgTopicRecommendPic;
		TextView tvRecommendName;
		TextView tvRecommendReson;
		TextView tvCheckFile;
		MLRecommendUser item;
		private View parent;
		String userName;

		private ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgTopicRecommendPic = (ImageView) parent
					.findViewById(R.id.imgTopicRecommendPic);
			tvRecommendName = (TextView) parent
					.findViewById(R.id.tvRecommendName);
			tvRecommendReson = (TextView) parent
					.findViewById(R.id.tvRecommendReson);
			tvCheckFile = (TextView) parent.findViewById(R.id.tvCheckFile);
			tvCheckFile.setOnClickListener(this);
		}

		public void refreshView(MLRecommendUser item, int i) {
			this.item = item;
			MLUser mlUser = new MLUser();
			mlUser = item.getUser();
			if (mlUser != null) {
				userName = mlUser.getNickname();
				tvRecommendName.setText("推荐" + NumberToChinese.convert(i + 1)
						+ ":" + userName);
			}
			tvRecommendReson.setText(item.getIntro());
			String avatar = item.getImages().get(0).getUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displayImage(avatar, imgTopicRecommendPic);
			} else {
				imgTopicRecommendPic
						.setImageResource(R.drawable.img_user_info_bg);
			}

		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tvCheckFile:
				String selectedUserId = item.getUser().getObjectId();
				boolean isLiked = MLContext.getUserLikeID(selectedUserId) == 0 ? false
						: true;
				Intent intent;
				if (MLApplication.getInstance().isLogin == true)
					intent = new Intent(ctx, Profile.class);
				else
					intent = new Intent(ctx, TempProfile.class);
				// Intent intent = new Intent(ctx, UserProfile.class);
				intent.putExtra("userId", selectedUserId);
				intent.putExtra("isLiked", isLiked);
				ctx.startActivity(intent);
				break;
			}
		}
	}
}
